package com.adee.netty.demo;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.nio.charset.Charset;

public class NettyByteBuf02 {
    public static void main(String[] args) {
        // 创建ByteBuf
        ByteBuf buf = Unpooled.copiedBuffer("hello, world!", Charset.forName("utf-8"));
        // 使用相关方法
        if(buf.hasArray()) {
            byte[] content = buf.array();
            System.out.println(new String(content, Charset.forName("utf-8")));
            System.out.println("buf=" + buf); // UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 0, widx: 13, cap: 39)
            System.out.println(buf.arrayOffset()); // 0
            System.out.println(buf.readerIndex()); // 0
            System.out.println(buf.writerIndex()); // 13
            System.out.println(buf.capacity()); // 39
            System.out.println(buf.readByte()); // 104
            int len = buf.readableBytes(); //
            System.out.println("len=" + len); // len=12
            for(int i = 0; i < len; i++) {
                System.out.println((char)buf.getByte(i)); // hello, world!
            }
            System.out.println(buf.getCharSequence(4, 6, Charset.forName("utf-8"))); // o, wor
        }
    }
}
